package com.kjtpay.design_pattern.strategy;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.simple_factory.SimpleFactoryActivity;

public class StrategyActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, StrategyActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    private EditText priceEditText;
    private EditText countEditText;
    private TextView totalTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strategy);
        setTitle("策略模式");

        priceEditText = (EditText) findViewById(R.id.priceEditText);
        countEditText = (EditText) findViewById(R.id.countEditText);
        totalTextView = (TextView) findViewById(R.id.totalTextView);
    }

    public void onReturnButtonClick(View view) {
        doCash("满300返100");
    }

    public void onRebateButtonClick(View view) {
        doCash("打8折");
    }

    public void onNormalButtonClick(View view) {
        doCash("普通收费");
    }

    private void doCash(String type) {
        try {
            double price = Double.parseDouble(priceEditText.getText().toString());
            int count = Integer.parseInt(countEditText.getText().toString());
            double money = price * count;
            CashContext cashContext = new CashContext(type);
            double result = cashContext.getResult(money);
            this.totalTextView.setText(String.valueOf(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}